package uz.rt.dicom.data;

import uz.rt.dicom.Padding;
import uz.rt.dicom.dictionary.DicomDictionary;
import uz.rt.dicom.tag.DicomTag;

public abstract class StringElement extends DataElement<ComparableList<String>>
{
// ------------------------------ FIELDS ------------------------------

    /** Character set */
    protected String characterSet = "ASCII";
    /** Padding */
    private Padding padding = Padding.NONE;

// --------------------------- CONSTRUCTORS ---------------------------

    protected StringElement(DicomTag tag)
    {
        super(tag);
        vm = DicomDictionary.getInstance().getTagVM(tag);
    }

// --------------------- GETTER / SETTER METHODS ---------------------

    /**
     * Get the String character set
     *
     * @return String character set
     */
    public String getCharacterSet()
    {
        return characterSet;
    }

    /**
     * Set the String character set
     *
     * @param characterSet String character set
     */
    public void setCharacterSet(String characterSet)
    {
        this.characterSet = characterSet;
    }

    /**
     * Get element padding
     *
     * @return Padding type
     */
    public Padding getPadding()
    {
        return padding;
    }

    /**
     * Set the type of padding
     *
     * @param padding Padding type
     */
    public void setPadding(Padding padding)
    {
        this.padding = padding;
    }

    public ComparableList<String> getValue()
    {
        return value;
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface IDataElement ---------------------

    /**
     * The function compares only the following parameters in order:
     * <ul>
     * <li>Dicom tag</li>
     * <li>Value multiplicity</li>
     * <li>Comparable list of data fields</li>
     * </ul>
     *
     * @param o IDataElement which has to contain a ComparableList value
     * @return A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
     *         the specified object.
     */
    /**
     * public int compareTo(DataElement o)
     * {
     * if (!tag.equals(o.getTag())) return tag.compareTo(o.getTag());
     * if (!vm.equals(o.getVm())) return vm.compareTo(o.getVm());
     * return value.compareTo((ComparableList) o.getValue());
     * }
     */

// --------------------- Interface IDataElement ---------------------
    @Override
    public int getLength()
    {
        return value.size();
    }

    public boolean isLengthFixed()
    {
        return false;
    }

// -------------------------- OTHER METHODS --------------------------

    /**
     * Splits a string into multiple strings based on the delimiter. If the delimiter is not present in the string an
     * array of Strings with length 1 is returned.
     *
     * @param s String to be splitted
     * @return An array of strings
     */
    protected String[] splitValues(String s)
    {
        String[] groups;
        // Workaround for a backslash problem related with
        if (s.contains(delimiter) && delimiter.equals("\\"))
            groups = s.split(delimiter + delimiter);
        else if (s.contains(delimiter) && !delimiter.equals("\\"))
            groups = s.split(delimiter);
        else
        {
            groups = new String[1];
            groups[0] = s;
        }
        return groups;
    }
}
